package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService empService;

    @PostMapping("/login")
    public R<Employee> selectByUsernameAndPwd(HttpServletRequest req, @RequestBody Employee employee){
        //①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password=employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //②. 根据页面提交的用户名username查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = empService.getOne(queryWrapper);

        //③. 如果没有查询到, 则返回登录失败结果
        if (null==emp){
            return R.error("登入失败");
        }
        //④. 密码比对，如果不一致, 则返回登录失败结果
        queryWrapper.eq(Employee::getPassword,password);
        emp=empService.getOne(queryWrapper);
        if (null==emp){
            return R.error("登入失败");
        }

        //⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus()==0){
            return R.error("员工已禁用");
        }
        //⑥. 登录成功，将员工id存入Session, 并返回登录成功结果
        req.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);

    }

    @PostMapping("/logout")
    public R empOut(HttpServletRequest req){

        req.getSession().removeAttribute("employee");

        return R.success("成功退出");
    }
    @PostMapping
    public R<String> save(HttpServletRequest req,@RequestBody Employee employee){
        //log.info("新增员工，员工信息：{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long id = (Long) req.getSession().getAttribute("employee");
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        empService.save(employee);
        return R.success("新增员工成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page p=new Page(page,pageSize);

        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper();
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lqw.orderByDesc(Employee::getUpdateTime);
        Page pg = empService.page(p, lqw);
        return R.success(pg);
    }
    @PutMapping
    public R<String> update(HttpServletRequest req,@RequestBody Employee employee){
        employee.setUpdateTime(LocalDateTime.now());
        Long user = (Long) req.getSession().getAttribute("employee");
        employee.setUpdateUser(user);
        empService.updateById(employee);
        return R.success("员工信息修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id){
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper();
        lqw.eq(Employee::getId,id);
        Employee one = empService.getOne(lqw);
        return R.success(one);
    }

}
